package com.graducationdesign.springboot.controller.Admin;

import com.graducationdesign.springboot.entities.Administrator.PersonnalInformationMoudle.Teacher;
import com.graducationdesign.springboot.mapper.TeacherMapper;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author Mr.zhneg
 * @create 2019-11-11-21:49
 */
@Controller
public class TeacherInformationController {

    org.slf4j.Logger logger=  LoggerFactory.getLogger(getClass());

    @Autowired
    TeacherMapper teacherMapper;

    //查看老师信息管理的主界面，并且可以查询老师名字
    @GetMapping("/teacher")
    public String list(Teacher teacher, Map<String,Object> map){

        logger.info("老师信息"+teacher);
       List<Teacher> teachers= teacherMapper.getTeachers(teacher);
        logger.info("查询操作：老师信息查询结果。。。"+teachers);

        map.put("teachers",teachers);
       map.put("teachername",teacher.getName());


        return "Administrator/personnalInformationModule/teacherinformationmanagement";
    }

    //查看老师信息，和前往老师修改页面
    @GetMapping("/teachers/{tid}")
    public String view(Map<String,Object> map, @PathVariable("tid") Integer tid, @RequestParam(value = "type",defaultValue = "teacherinformationmanagementview") String type) {

        logger.info("查询操作：查询老师编码为"+tid+"的老师信息");
        Teacher teachers=teacherMapper.getTeacherBytid(tid);
        logger.info("查询老师信息的结果："+teachers);

        map.put("teachers",teachers);

//        return "Administrator/personnalInformationModule/teacherinformationmanagementview";
        return "Administrator/personnalInformationModule/"+type;
    }

    //修改老师信息
    @PutMapping("/teachers")
    public String update(Teacher teacher,Integer tid){

        logger.info("修改操作：修改老师编码为"+tid+"的老师信息");
        teacherMapper.updateTeacher(teacher);
        return "redirect:teacher";
    }

    //来到添加老师页面
    @GetMapping("/teachers")
    public String toAddPage(){

        return "Administrator/personnalInformationModule/teacherinformationmanagementadd";
    }

    //    在添加老师页面中添加老师信息
    @PostMapping("/teachers")
    public String add(Teacher teacher,Integer id){

        logger.info("添加操作：添加编号为"+id+"的老师信息"+teacher);
        teacherMapper.addTeacher(teacher);

        return "redirect:teacher";
    }

    //删除老师信息
    @DeleteMapping("/teachers/{tid}")
    public String delete(@PathVariable("tid") Integer tid){

        logger.info("删除操作：删除编号为"+tid+"的老师");
        teacherMapper.deleteStudentById(tid);
        return "redirect:/teacher";

     }

}
